package com.basaji.manager.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.basaji.dao.ITopicDao;
import com.basaji.dao.manager.PojoManager;
import com.basaji.domain.pojo.Topic;
import com.basaji.manager.ITopicManager;
import com.basaji.util.CheckDomain;

public class TopicManager extends BaseManager implements ITopicManager {

	private ITopicDao topicDao;
	
	Logger LOG = Logger.getLogger(TopicManager.class);
	
	@Override
	public List<Topic> getReplies(int id, int start, int size) {
		try {
			return topicDao.getReplies(id, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<Topic> getTopics(int classificationId, int start, int size) {
		try {
			return topicDao.getTopics(classificationId, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<Topic> getTopicsByItem(int itemId, int start, int size) {
		try {
			return topicDao.getTopicsByItem(itemId, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<Topic> getUserTopics(int uid, int start, int size) {
		try {
			return topicDao.getUserTopics(uid, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getTopicCount(int classifcationId) {
		try {
			return topicDao.getTopicCount(classifcationId);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public int getTopicCountByItem(int itemId) {
		try {
			return topicDao.getTopicCountByItem(itemId);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public int getUserTopicCount(int uid) {
		try {
			return topicDao.getUserTopicCount(uid);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	public ITopicDao getTopicDao() {
		return topicDao;
	}

	public void setTopicDao(ITopicDao topicDao) {
		this.topicDao = topicDao;
	}

	@Override
	public int saveTopic(Topic topic) {
		int result = CheckDomain.checkTopic(topic) ;
		if(result <= 0){
			return result;
		}
		Date _now = new Date();
		topic.setCreated(_now);
		topic.setReplied(_now);
		save(topic);
		return topic.getId();
	}

	@Override
	public void deleteTopic(int id) {
		Topic _topic = (Topic) get(Topic.class,id);
		if(_topic != null){
			delete(_topic);
		}
		
	}

	@Override
	public int saveReply(Topic reply) {
		int result = CheckDomain.checkTopic(reply) ;
		if(result <= 0){
			return result;
		}
		Date _now = new Date();
		Topic _topic = (Topic) get(Topic.class,reply.getParentId());
		_topic.setReplyCount(_topic.getReplyCount()+1);
		_topic.setReplied(_now);
		update(_topic);
		
		reply.setCreated(_now);
		save(reply);
		return reply.getId();
	}

	@Override
	public void deleteReply(int id) {
		Topic _reply = (Topic) get(Topic.class,id);
		if(_reply != null && _reply.getParentId() != 0){
			Topic _topic = (Topic) get(Topic.class,_reply.getParentId());
			_topic.setReplyCount(_topic.getReplyCount() - 1);
			update(_topic);
			delete(_reply);
		}
	}

}
